package com.wanmait.shoppingsys.util;
import java.util.ArrayList;
import java.util.List;

import com.wanmait.shoppingsys.vo.Specialty;

public class ShopCar {
	private ArrayList<Specialty> specialtys = new ArrayList<Specialty>();
	
	public void clear() {     //清空购物车
		specialtys.clear();
	}
	
	public void remove(Integer id) {     //删除商品
		for(Specialty b:specialtys) {
			if(b.getId().equals(id)) {
				specialtys.remove(b);
				break;
			}
		}
	}
	
	public int addCount(Integer id,int count) {  //点商品加号
		for(Specialty b:specialtys) {
			if(b.getId().equals(id)) {
				b.setCount(b.getCount()+count);
				if(b.getCount()<0)b.setCount(0);
				return b.getCount();
			}
		}
		return 0;
	}
	
	
	public Double getTotalPrice() {   //购物车里物品总钱数
		
		double total = 0.0;
		
		for(Specialty b:specialtys) {
			
			total+= b.getPrice()*b.getCount();
			
			
			
		}
		return total;
	}
	
	public ArrayList getPrice() {   //单品价格
		ArrayList danpin =new ArrayList();
		double total = 0.0;
		
		for(Specialty b:specialtys) {
			
			total+= b.getPrice()*b.getCount();
			danpin.add(total);
			
			
		}
		return danpin;
	}
	public int getTotalNumber() {                //购物车里面商品数量
		int num = 0;
		for(Specialty b:specialtys) {
			num+=b.getCount();
		}
		return num;
	}
	
	  public void addCommodity (Specialty specialty)    //往购物车添加商品
	  {
		  boolean find = false;
		  for(Specialty b:specialtys)
		  {
			  if(b.getId().equals(specialty.getId()))
			  {
				 b.setCount(b.getCount()+specialty.getCount()); 
				 find = true;
				 break;
			  }
		  }
		  if(!find)
		  {
			  specialtys.add(specialty) ; 
		  }
	  }
	  
	  public List<Specialty> getSpecialtys()
	  {
		  return  specialtys;
	  }
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
	  
}
